package com.thghh.fsbrowser.javafx;

import java.io.File;
import java.net.URL;
import java.util.ResourceBundle;

import org.apache.commons.lang3.time.DateFormatUtils;

import com.thghh.fsbrowser.domain.FileInfo;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import lombok.Getter;

/**
 * com.thghh.fsbrowser.javafx.FileSystemController
 * 
 * @author pengzhikang
 * @date 2018年9月21日下午10:55:32
 * @since 1.0
 */
@Getter
public class FileSystemController implements Initializable {
    
    private MainController MainController;
    
    @FXML
    private Button btUndo;

    @FXML
    private Button btRedo;

    @FXML
    private Button buttonUpload;

    @FXML
    private Button buttonDownload;

    @FXML
    private Button buttonAddDirectory;

    @FXML
    private Button buttonUpdate;

    @FXML
    private Button buttonDelete;

    @FXML
    private TextField textFieldSearch;

    @FXML
    private Button buttonAddResetConnect;

    @FXML
    private TabPane fileTabPane;

    @FXML
    private Tab fileTab;
    
    @FXML
    private TextField txtFilePath;

    @FXML
    private TableView<FileInfo> fileTabView;

    @FXML
    private TableColumn<?, ?> columnFileName;

    @FXML
    private TableColumn<?, ?> columnFileSize;

    @FXML
    private TableColumn<?, ?> columnFileType;

    @FXML
    private TableColumn<?, ?> columnFileRule;

    @FXML
    private TableColumn<?, ?> columnFileUser;

    @FXML
    private TableColumn<?, ?> columnFileDate;

    @FXML
    private Button btStatus;

    @FXML
    private Label lbLeftStatus;

    @FXML
    private Label lbRightStatus;

    public FileSystemController() {
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
    	File[] files = File.listRoots();
    	columnFileName.setCellValueFactory(new PropertyValueFactory<>("name"));
    	columnFileSize.setCellValueFactory(new PropertyValueFactory<>("size"));
    	columnFileType.setCellValueFactory(new PropertyValueFactory<>("type"));
    	columnFileRule.setCellValueFactory(new PropertyValueFactory<>("rule"));
    	columnFileUser.setCellValueFactory(new PropertyValueFactory<>("user"));
    	columnFileDate.setCellValueFactory(new PropertyValueFactory<>("date"));
    	
    	fileTabView.setEditable(true);
        fileTabView.setItems(getData(files[0]));
    }
    
    private ObservableList<FileInfo> getData(File f) {
    	txtFilePath.setText(f.getPath());
    	fileTab.setText(f.getPath());
    	ObservableList<FileInfo> list = FXCollections.observableArrayList();
    	File[] files = f.listFiles();
    	for (File file : files) {
			FileInfo fi = new FileInfo();
			fi.setName(file.getName());
			fi.setSize(file.length());
			fi.setType(file.isFile() ? "File" : "Dir");
			fi.setUser("");
			fi.setRule(file.canWrite() ? "W" : "R");
			fi.setDate(DateFormatUtils.format(file.lastModified(), "yyyy-MM-dd HH:mm:ss"));
			list.add(fi);
		}
    	return list;
    }
}
